package com.jxsa.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxsa.entity.CustomUser;
import com.jxsa.entity.SysUser;
import com.jxsa.mapper.SysUserMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pwq
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserDetailsServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements UserDetailsService {
    @Override
    public CustomUser loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,s);
        SysUser sysUser = this.baseMapper.selectOne(wrapper);
        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new SimpleGrantedAuthority("ADMIN"));
        CustomUser customUser = new CustomUser(s, sysUser.getPassword(), list);
        customUser.setId(sysUser.getId());
        return customUser;
    }
}
